package com.cloud.translation.Fragment;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cloud.translation.Adapter.HistoryAdapter;
import com.cloud.translation.BaseUI.BaseFragment;
import com.cloud.translation.Dao.DaoUtils;
import com.cloud.translation.Model.Model;
import com.cloud.translation.R;
import com.cloud.translation.observable.AndroidObervable;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class HistoryFragment extends BaseFragment implements Observer {

    private RecyclerView recyclerView;

    private List<Model> historyModels;

    private HistoryAdapter historyAdapter;

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        AndroidObervable.getInstance().addObserver(this);
        recyclerView = view.findViewById(R.id.recycler);
        historyModels=new DaoUtils(getContext()).queryAll();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_history;
    }

    @Override
    protected void setEvent() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        historyAdapter=new HistoryAdapter(getActivity());
        recyclerView.setAdapter(historyAdapter);
        historyAdapter.setData(historyModels);
    }



    @Override
    protected void getData() {
    }

    @Override
    public void onDetach() {
        super.onDetach();
        AndroidObervable.getInstance().deleteObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        historyAdapter.setData(new ArrayList<>());
    }
}
